package com.example.pungolo.journal;

import com.example.pungolo.journal.view.CroppableImageView;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class CropActivity extends Activity {

	private CroppableImageView image;

  @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_crop);
		Intent caller=getIntent();
		
    if(caller == null || !caller.hasExtra("picturePath")) throw new IllegalStateException();
    
    String path = caller.getStringExtra("picturePath");
    Bitmap bm = BitmapFactory.decodeFile(path);
		this.image = (CroppableImageView)findViewById(R.id.cropUI_imageView);
	  this.image.setBitmap(bm);

    Button saveButton = (Button) findViewById(R.id.cropUI_savebutton);
    Button cancelButton = (Button) findViewById(R.id.cropUI_cancelButton);

    saveButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { onSaveClicked(); } });
    cancelButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { onCancelClicked(); } });
	}

  protected void onCancelClicked() {
    setResult(RESULT_CANCELED);
    finish();
  }

  protected void onSaveClicked() {
    Intent back = new Intent();
    int[] cropData = new int[4];
    this.image.getCropData(cropData);
    back.putExtra("cropData", cropData);
    
    setResult(RESULT_OK, back);
    finish();
  }

}
